package com.example.othree.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.othree.entity.Drone;
import com.example.othree.entity.DroneCount;
import com.example.othree.mapper.DroneCountMapper;
import com.example.othree.mapper.DroneManagementMapper;
import com.example.othree.mapper.DroneMapper;
import com.example.othree.service.DroneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DroneServiceImpl extends ServiceImpl<DroneCountMapper, DroneCount>  implements DroneService {

    @Autowired
    DroneMapper droneMapper;

    @Autowired
    DroneCountMapper droneCountMapper;

    //无人机海拔异常数据限制条件
    @Value("${data.drone.elevationLimit}")
    Integer elevationLimit;

    @Override
    public List<DroneCount> toFindByEquipmentName(String equipmentName) {
        QueryWrapper<DroneCount> wrapper = new QueryWrapper<>();

        return droneCountMapper.selectList(wrapper.like("equipment_name", equipmentName));
    }

    @Override
    public Map<String, Object> toFindByNameAndDate(String equipmentName, String startTime, String endTime, Boolean isCorrected, int page, int pageSize) {

        Page<Drone> pageList = new Page<>(page,pageSize);

        List<Drone> droneList;

        if(isCorrected){
            droneList = droneMapper.correctedList(pageList, equipmentName, startTime, endTime, elevationLimit);


        }else {
            droneList = droneMapper.notCorrectedList(pageList, equipmentName, startTime, endTime, elevationLimit);
        };

        Map<String, Object> returnData = new HashMap<String, Object>();
        returnData.put("total", pageList.getTotal());
        returnData.put("currentPage", pageList.getCurrent());
        returnData.put("pageSize", pageList.getSize());
        returnData.put("totalPages", pageList.getPages());
        returnData.put("data", droneList);

        return returnData;
    }

    @Override
    public ArrayList<Map> toFindDataFixed(String equipmentName, String time) {
        QueryWrapper<Drone> wrapper = new QueryWrapper<>();

        List<Drone> droneList = droneMapper.selectList(wrapper.like("equipment_name", equipmentName)
                .eq("detection_time", time)
                .groupBy("detection_time, elevation")
                .select("AVG(lon) as lon, AVG(lat) as lat, AVG(temp) as temp, elevation," +
                        "AVG(Pm1_0) as Pm1_0, AVG(Pm2_5) as Pm2_5, AVG(pm10) as Pm10," +
                        "AVG(humidity) as humidity,AVG(O3) as O3"));

        ArrayList<Map> droneListDTO = new ArrayList<Map>();
        for(Drone drone : droneList){
            Map<String, Object> droneDTO = new HashMap<>();
            droneDTO.put("lon", drone.getLon());
            droneDTO.put("lat", drone.getLat());
            droneDTO.put("temp", drone.getTemp());
            droneDTO.put("elevation", drone.getElevation());
            droneDTO.put("Pm1_0", drone.getPm1_0());
            droneDTO.put("Pm2_5", drone.getPm2_5());
            droneDTO.put("Pm10", drone.getPm10());
            droneDTO.put("humidity", drone.getHumidity());
            droneDTO.put("O3", drone.getO3());

            droneListDTO.add(droneDTO);
        }

        return droneListDTO;
    }

    @Override
    public Integer toFindCountByEquipmentName(String equipmentName) {
        QueryWrapper<DroneCount> wrapper = new QueryWrapper<>();
        wrapper.eq("equipment_name", equipmentName);
        return droneCountMapper.selectCount(wrapper);
    }

    @Override
    public ArrayList<String> toFindRecentFly() {
        ArrayList res = new ArrayList<String>();
        QueryWrapper<DroneCount> wrapper1 = new QueryWrapper<>();
        QueryWrapper<DroneCount> wrapper2 = new QueryWrapper<>();

        wrapper1.eq("equipment_name", "内丘县气象站");
        wrapper2.eq("equipment_name", "饶阳县气象站");

        wrapper1.orderByDesc("start_time");
        wrapper2.orderByDesc("start_time");

        DroneCount droneCount1 = droneCountMapper.selectList(wrapper1).get(0);
        DroneCount droneCount2 = droneCountMapper.selectList(wrapper2).get(0);

        res.add(droneCount1.getStartTime());
        res.add(droneCount2.getStartTime());

        return res;
    }

}
